## -*- coding: utf-8 -*-

import sys
from bs4 import BeautifulSoup
import sqlite3
import re
import urllib.request, urllib.error
from urllib.parse import quote
import xlwt

findImageSrc = re.compile(r'<div class="p-img"> <a target="_blank" title href="([\w\W]+?)"') # re.S让换行符包含在字符中

# 得到指定一个URL的网页内容
def askURl(url):
	head = {"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36"}
	request = urllib.request.Request(url, headers = head)
	html = ""
	try:
		response = urllib.request.urlopen(request)
		html = response.read().decode("utf-8")
	except urllib.error.URLError as e:
		if hasattr(e, "code"):
			print("请求出错",e.code)
		if hasattr(e, "reason"):
			print("错误原因",e.reason)
	return html

def getData(base_url):
	html = askURl(base_url)
	
	# 2. 逐一解析网页
	soup = BeautifulSoup(html, "html.parser")
	for item in soup.find_all("div", class_="gl-i-wrap"):
		img = item.div.a["href"]
		prince = item.strong.i.string
		name = item.find("div", class_="p-name p-name-type-2").em.text
		commit = item.find("div", class_="p-commit") # 失败
		try:
			shop = item.find("div", class_="p-shop").a.string
			print("commit: ",commit)
		except Exception as e:
			print('tiaoguo')



def main():

	base_url = "https://search.jd.com/Search?keyword=" +quote("手机") + "&enc=utf-8"
	# 1. 获取网页
	data_list = getData(base_url)

main()